<template>
  <div class="teachers_page">
    <div style="display:flex;align-items: center;justify-content: space-between;margin:0 0 10px;">
      <div style="display:flex;align-items: center;">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="试卷ID">
            <el-input clearable size="small" v-model="formInline.id" placeholder="试卷ID"></el-input>
          </el-form-item>
          <el-form-item label="试卷名称">
            <el-input clearable size="small" v-model="formInline.name" placeholder="试卷名称"></el-input>
          </el-form-item>
          <el-form-item label="批卷教师">
            <el-input clearable size="small" v-model="formInline.teacher" placeholder="批卷教师"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select clearable size="small" v-model="formInline.state" placeholder="请选择">
              <el-option
                v-for="item in [{
                  value: '1',
                  label: '启用'
                },{
                  value: '0',
                  label: '禁用'
                },]"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-button size="small" type="primary" @click="onSubmit">搜索</el-button>
        <el-button size="small" @click="reset">重置</el-button>
      </div>
      <div>
      <el-button size="small" type="primary" @click="add">创建作业试卷</el-button>
    </div>
  </div>
  <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
    <el-table-column label="ID" width="100">
      <template slot-scope="scope">
        {{ scope.row.id }}
      </template>
    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" label="试卷名称" width="200">
      <template slot-scope="scope">
        {{ scope.row.paperName }}
      </template>
    </el-table-column>
    <el-table-column label="试卷海报" width="140">
      <template slot-scope="scope">
        <img :src="scope.row.picImageUrl" style="width:100px;height:60px" alt="">
      </template>
    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" label="试卷描述" width="200">
      <template slot-scope="scope">
        {{ scope.row.description }}
      </template>
    </el-table-column>
    <el-table-column label="判卷教师" width="100">
      <template slot-scope="scope">
        {{ scope.row.markTeacherName }}
      </template>
    </el-table-column>
    <el-table-column label="答题时长" width="100">
      <template slot-scope="scope">
        {{ scope.row.answerTime }}
      </template>
    </el-table-column>
    <el-table-column label="试卷类型" width="100">
      <template>
        作业试卷
      </template>
    </el-table-column>
    <el-table-column label="单选题" width="100">
      <template slot-scope="scope">
        <el-tag @click="openQuestionList(scope.row, 1)" size="small" style="cursor:pointer;">{{ scope.row.singleChoiceCount?scope.row.singleChoiceCount:0 }}</el-tag> * {{ scope.row.singleChoiceScore?scope.row.singleChoiceScore:0 }}分
      </template>
    </el-table-column>
    <el-table-column label="多选题" width="100">
      <template slot-scope="scope">
        <el-tag @click="openQuestionList(scope.row, 2)" size="small" style="cursor:pointer;">{{ scope.row.multipleChoiceCount?scope.row.multipleChoiceCount:0 }}</el-tag> * {{ scope.row.multipleChoiceScore?scope.row.multipleChoiceScore:0 }}分
      </template>
    </el-table-column>
    <el-table-column label="判断题" width="100">
      <template slot-scope="scope">
        <el-tag @click="openQuestionList(scope.row, 3)" size="small" style="cursor:pointer;">{{ scope.row.judgmentCount?scope.row.judgmentCount:0 }}</el-tag> * {{ scope.row.judgmentScore?scope.row.judgmentScore:0 }}分
      </template>
    </el-table-column>
    <el-table-column label="简答题" width="100">
      <template slot-scope="scope">
        <el-tag @click="openQuestionList(scope.row, 4)" size="small" style="cursor:pointer;">{{ scope.row.shortAnswerCount?scope.row.shortAnswerCount:0 }}</el-tag> * {{ scope.row.shortAnswerScore?scope.row.shortAnswerScore:0 }}分
      </template>
    </el-table-column>
    <el-table-column label="总分" width="100">
      <template slot-scope="scope">
        {{ scope.row.score }}
      </template>
    </el-table-column>
    <!-- <el-table-column label="创建人" width="100">
      <template slot-scope="scope">
        {{ scope.row.person }}
      </template>
    </el-table-column> -->
    <el-table-column label="创建时间" width="200">
      <template slot-scope="scope">
        {{ scope.row.createTime }}
      </template>
    </el-table-column>
    <el-table-column label="状态" width="100">
      <template slot-scope="scope">
        <el-tag style="cursor:pointer;" size="small" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
        <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="150">
      <template slot-scope="scope">
        <el-button @click="changeState(scope.row, 0)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">禁用</el-button>
        <el-button @click="changeState(scope.row, 1)" type="text" v-else>启用</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-button @click="edit(scope.row)" type="text">编辑</el-button>
        <el-divider direction="vertical"></el-divider>
        <el-popconfirm
          @onConfirm="del(scope.row)"
          confirm-button-text='确定'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="是否删除本条数据？">
          <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin:10px 0 0 0;text-align:right;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageObj.pageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="pageObj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageObj.total">
    </el-pagination>
  </div>

  <el-dialog width="1200px" top="10vh" :title="questionModalTitle" :visible.sync="questionModal">
    <el-table style="width: 100%" height="500" ref="multipleTable" v-loading="questionLoading" :data="questionList" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
      <el-table-column label="ID" width="150">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="题目名称">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column v-if="questionType == 1 || questionType == 2" :show-overflow-tooltip="true" label="题目选项" width="200">
        <template slot-scope="scope">
          <div v-html="scope.row.options">
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="questionType != 4" label="题目答案" width="200">
        <template slot-scope="scope">
          {{ scope.row.answer }}
        </template>
      </el-table-column>
      <el-table-column label="题目类型" width="200">
        <template>
          <span v-if="questionType == 1">
            单选题
          </span>
          <span v-else-if="questionType == 2">
            多选题
          </span>
          <span v-else-if="questionType == 3">
            判断题
          </span>
          <span v-else>
            简答题
          </span>
        </template>
      </el-table-column>
      <el-table-column label="答案解析" width="200">
        <template slot-scope="scope">
          {{ scope.row.analysis }}
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer" style="text-align:center;">
      <el-button size="small" type="primary" @click="closeDialog">关 闭</el-button>
    </div>
  </el-dialog>

</div>
</template>

<script>
import {
  getListApi,
  deleteApi,
  changeStateApi,
  getPaperListApi
} from '@/api/paper'

export default {
  data() {
    return {
      list: [],
      listLoading: false,
      currentPage: 1,
      formInline: {
        id: '',
        name: '',
        teacher: ''
      },
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      questionModal: false,
      questionList: [],
      questionLoading: false,
      questionType: 1,
      questionModalTitle: ''
    }
  },
  created() {
    if(this.$route.query.id) {
      this.formInline.id = this.$route.query.id
      this.formInline.name = this.$route.query.name
    }
    this.fetchData()
  },
  methods: {
    openQuestionList(row, type) {
      this.questionModal = true
      this.questionType = type
      if(this.questionType == 1) {
        this.questionModalTitle = '单选题'
      } else if(this.questionType == 2) {
        this.questionModalTitle = '多选题'
      } else if(this.questionType == 3) {
        this.questionModalTitle = '判断题'
      } else if(this.questionType == 4) {
        this.questionModalTitle = '简答题'
      }
      this.getQuestionList(row, type)
    },
    closeDialog() {
      this.questionModal = false
      this.questionList = []
    },
    getQuestionList(row, type) {
      this.questionLoading = true
      getPaperListApi({
        id: row.id,
        questionType: type
      }).then((res) => {
        this.questionLoading = false
        this.questionList = res.data
      })
    },
    onSubmit() {
      this.fetchData()
    },
    reset() {
      this.formInline.id = ''
      this.formInline.name = ''
      this.formInline.teacher = ''
      this.pageObj.pageSize = 10
      this.pageObj.pageNum = 1
      this.formInline.state = ''
      this.fetchData()
    },
    changeState(row, state) {
      changeStateApi({
        id: row.id,
        state: state
      }).then((res) => {
        this.fetchData()
      })
    },
    del(row) {
      deleteApi({
        ids: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    edit(row) {
      console.log(row)
      this.$router.push({
        path: '/paper/taskEdit',
        query: {
          editId: row.id
        }
      })
    },
    add(row) {
      console.log(row)
      this.$router.push({
        path: '/paper/taskAdd'
      })
    },
    fetchData() {
      this.listLoading = true
      getListApi({
        id: this.formInline.id,
        pageNum: this.pageObj.pageNum,
        pageSize: this.pageObj.pageSize,
        name: this.formInline.name,
        state: this.formInline.state,
        teacher: this.formInline.teacher,
        type: 0,
      }).then(response => {
        this.list = response.data.result
        this.listLoading = false
        this.pageObj.total = response.data.total
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style>
  .el-table .cell{
    white-space: pre-line;
  }
</style>

<style lang="scss" scoped>
   .teachers_page{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
